package com.service.impl;

import com.bean.BookReview;
import com.mapper.BookReviewMapper;
import com.service.BookReviewService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookReviewServiceImpl implements BookReviewService {
    @Autowired
    private BookReviewMapper bookReviewMapper;

    @Override
    public BookReview getBookReviewById(Integer reviewId) {
        return bookReviewMapper.getBookReviewById(reviewId);
    }

    @Override
    public PageInfo<BookReview> getReviewsByBookIdWithKeyword(Integer bookId, String keyword, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<BookReview> reviewList = bookReviewMapper.getReviewsByBookIdWithKeyword(bookId, keyword, offset, pageSize);
        int total = bookReviewMapper.countReviewsByBookIdWithKeyword(bookId, keyword);
        return new PageInfo<>(pageNum, pageSize, total, reviewList);
    }

    @Override
    public boolean insertBookReview(BookReview bookReview) {
        int result = bookReviewMapper.insertBookReview(bookReview);
        return result > 0;
    }

    @Override
    public boolean updateBookReview(BookReview bookReview) {
        int result = bookReviewMapper.updateBookReview(bookReview);
        return result > 0;
    }

    @Override
    public boolean deleteBookReviewById(Integer reviewId) {
        int result = bookReviewMapper.deleteBookReviewById(reviewId);
        return result > 0;
    }
}